<?php
	include_once('dbcon.php');
	include_once('constants.php');
	include_once('error_log_db.php');
	include_once('general_functions_db.php');
	
	function get_one_content($content_id, $user_id) {
		global $mysqli;
		
		$content_query = $mysqli->query("SELECT contents.*, categories.name as category_name FROM contents INNER JOIN categories ON contents.category_id = categories.category_id WHERE contents.content_id=".$content_id);
		logIfError($mysqli, $user_id, "get_contents_db", "get_one_content", "1");
		
		$content = buildContentArray($content_query->fetch_assoc(), $user_id);

		return json_encode($content, JSON_HEX_QUOT | JSON_HEX_APOS);
	}

	function get_user_content_db($limit, $from_id, $user_id, $get_not_anonymous) {
		global $mysqli;

		$anonymous_query = "";

		if ($get_not_anonymous == "1") {
			$anonymous_query = " && isAnonymous=0";
		}

		if ( $from_id != -1 ) {
			$content_query = $mysqli->query("SELECT * FROM contents WHERE content_id<".$from_id." && user_id=".$user_id.$anonymous_query." ORDER BY content_id DESC LIMIT ".$limit);
		} else {
			$content_query = $mysqli->query("SELECT * FROM contents WHERE user_id=".$user_id.$anonymous_query." ORDER BY content_id DESC LIMIT ".$limit);
		}

		logIfError($mysqli, $user_id, "get_contents_db", "get_user_content_db", "1");

		return return_content($content_query, $user_id);
	}

	function get_bests_lasts_content_db($limit, $from_id, $user_id, $get_best, $category_id) {
		global $mysqli;

		$from_condition = "";
		if ($from_id != -1) {
			$from_condition = "content_id<".$from_id;
		}

		$category_condition = "";
		if ($category_id != -1) {
			$category_condition = "contents.category_id=".$category_id;
		}

		$best_condition = "";
		if ($get_best == 1) {
			$best_condition = "contents.points>=".MIN_POINTS_TO_BESTS;
		}


		$condition = "";
		$conditions_array = array($from_condition, $category_condition, $best_condition);

		$where_added = false;
		for ($i = 0; $i < count($conditions_array); $i++) {
			if ($conditions_array[$i] != "") {
				if ($where_added == true) {
					$condition = $condition." && ";
				} else {
					$condition = "WHERE ";
					$where_added = true;
				}
				$condition = $condition.$conditions_array[$i];
			}
		}

		$content_query = $mysqli->query("SELECT contents.*, categories.name as category_name FROM contents INNER JOIN categories ON contents.category_id = categories.category_id ".$condition." ORDER BY content_id DESC LIMIT ".$limit);
		logIfError($mysqli, $user_id, "get_contents_db", "get_bests_lasts_content_db", "1");

		return return_content($content_query, $user_id);
	}

	function return_content($content_query, $user_id) {
		global $mysqli;
		$all_content = array();

		while ($row = $content_query->fetch_assoc()) {
			$content = buildContentArray($row, $user_id);
			
			array_push($all_content, $content);
		}
		
		return json_encode($all_content, JSON_HEX_QUOT | JSON_HEX_APOS);
	}

	function get_amount_contents($user_id, $count_anonymous) {
		global $mysqli;
		
		if ($user_id == -1)
			if ($count_anonymous == 1)
				$amount_query = $mysqli->query("SELECT COUNT(*) FROM contents");
			else
				$amount_query = $mysqli->query("SELECT COUNT(*) FROM contents WHERE isAnonymous=0");
		else
			if ($count_anonymous == 1)
				$amount_query = $mysqli->query("SELECT COUNT(*) FROM contents WHERE user_id=".$user_id);
			else
				$amount_query = $mysqli->query("SELECT COUNT(*) FROM contents WHERE user_id=".$user_id." && isAnonymous=0");

		logIfError($mysqli, $user_id, "get_contents_db", "get_amount_contents", "1");	
		
		return $amount_query->fetch_assoc()['COUNT(*)'];
	}

	if ( isset($_REQUEST['limit']) && isset($_REQUEST['user_id'])) {
		
		$activity="Get content requested. Limit=".$_REQUEST['limit'];
		
		if ( isset($_REQUEST['from_id']) ) 
			$activity=$activity." fromId=".$_REQUEST['from_id'];

		if ( isset($_REQUEST['get_best']) ) 
			$activity=$activity." get_best=".$_REQUEST['get_best'];

		logMsg($_REQUEST['user_id'], $activity);

		$category = isset($_REQUEST['category_id']) ? $_REQUEST['category_id'] : -1;
		$from_id = isset($_REQUEST['from_id']) ? $_REQUEST['from_id'] : -1;
		$get_not_anonymous = isset($_REQUEST['get_not_anonymous']) ? $_REQUEST['get_not_anonymous'] : -1;

		if (isset($_REQUEST['get_best'])) {
			echo get_bests_lasts_content_db($_REQUEST['limit'], $from_id, $_REQUEST['user_id'], $_REQUEST['get_best'], $category);
		} else {
			echo get_user_content_db($_REQUEST['limit'], $from_id, $_REQUEST['user_id'], $get_not_anonymous);
		}
		
	}
	
?>
